package servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.session.ILoginRemote;
import utils.SeU;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.process(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.process(request,response);
	}

	private void process(HttpServletRequest request,HttpServletResponse response) {
		try {
			Context c = new InitialContext();
		
			ILoginRemote l = (ILoginRemote)c.lookup("ILoginRemote");
			
			String user =request.getParameter(SeU.USERNAME_ATTRIBUTE);
			String pass = request.getParameter(SeU.PASSWORD_ATTRIBUTE);
			
			l.CreateUser
			(
					request.getParameter(SeU.NAME_ATTRIBUTE),
					request.getParameter(SeU.SURNAME_ATTRIBUTE),
					user,
					pass,
					request.getParameter(SeU.MAIL_ATTRIBUTE),
					""
			);
			
			response.sendRedirect(response.encodeURL("login?username="+user+"&password="+pass));
					
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}

}
